﻿/// <reference name="MicrosoftAjax.js"/>
/// <author name="Argo Zhang" msn="Argo@live.ca" />

Type.registerNamespace("Gammar.Price.Web");

Gammar.Price.Web.Compare = function(element) {
    Gammar.Price.Web.Compare.initializeBase(this, [element]);
    this._clickHandler = null;
    this._chkSelectControlName = "chkCompare";
    this._compareUrl = "";
}

Gammar.Price.Web.Compare.prototype = {
    initialize: function() {
        Gammar.Price.Web.Compare.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var element = this.get_element();
        this._clickHandler = Function.createDelegate(this, this._onClick);
        $addHandler(element, "click", this._clickHandler);
    },
    
    _onClick: function(eventObject) {
        var chkboxs = document.getElementsByName(this.get_CheckboxControlName());
        if (chkboxs.length > 0) {
            if (this.get_CompareUrl().length > 0) {
                var params = "";
                for (var i = 0;i<chkboxs.length;i++) {
                    if (chkboxs[i].checked) {
                        params += String.format("&id{0}={1}", i, chkboxs[i].getAttribute("guid"));
                    }
                }
                var url = String.format("{0}?{1}", this.get_CompareUrl(), params.substr(1, params.length - 1));
                window.open(url, "_blank");
            }
            else {
                alert("未设置比较页面！");
            }
        }
        else {
            alert("请选择您要比对的课程");
        }
    },
    
    set_CompareUrl: function(value) {
        /// <summary />
        /// <param name="value" type="String" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: String}
        ]);
        if (e) throw e;        
        this._compareUrl = value;
    },
    
    get_CompareUrl: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._compareUrl;
    },
    
    set_CheckboxControlName: function(value) {
        /// <summary />
        /// <param name="value" type="String" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: String}
        ]);
        if (e) throw e;        
        this._chkSelectControlName = value;
    },
    
    get_CheckboxControlName: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._chkSelectControlName;
    },

    dispose: function() {        
        //Add custom dispose actions here
        if (this._clickHandler !== null)
            $removeHandler(this.get_element(), "click", this._clickHandler);
        Gammar.Price.Web.Compare.callBaseMethod(this, 'dispose');
    }
}
Gammar.Price.Web.Compare.registerClass('Gammar.Price.Web.Compare', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
